//
//  MainViewController.m
//  XMDLearn
//
//  Created by hao on 16/4/8.
//  Copyright © 2016年 hao. All rights reserved.
//

#import "MainViewController.h"
#import "SDCycleScrollView.h"
#import "UploadViewController.h"
#import "DownloadViewController.h"


@interface MainViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>

@property (retain, nonatomic)UISearchBar *searchBar;

@property (nonnull,retain)UIAlertView *alertview;

@property (strong,nonatomic)UITableView *tableview;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGestureRecognizer.cancelsTouchesInView = NO;
    //将触摸事件添加到当前view
    [self.view addGestureRecognizer:tapGestureRecognizer];
    
    CGFloat w = self.view.bounds.size.width;
    
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w*0.6, 35)];//allocate titleView
    UIColor *color =  self.navigationController.navigationBar.barTintColor;
    
    [titleView setBackgroundColor:color];
    
    _searchBar = [[UISearchBar alloc] init];
    _searchBar.delegate = self;
    _searchBar.frame = CGRectMake(0, 0, w*0.6, 35);
    _searchBar.backgroundColor = color;
    _searchBar.layer.cornerRadius = 18;
    _searchBar.layer.masksToBounds = YES;
    [_searchBar.layer setBorderWidth:4];
    [_searchBar.layer setBorderColor:[UIColor redColor].CGColor];  //设置边框为白色
    
    _searchBar.placeholder = @"搜索你想要的东西";
    [titleView addSubview:_searchBar];
    //Set to titleView
    [self.navigationItem.titleView sizeToFit];
    self.navigationItem.titleView = titleView;
    
  //右边两个按钮的创建
    UIView *rightBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 31)];
    
    UIButton *searchbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    searchbutton.frame=CGRectMake(10, 5, 25, 25);
    [searchbutton setImage:[UIImage imageNamed:@"topbar_icon_search_pressed"] forState:UIControlStateNormal];
    [searchbutton setImage:[UIImage imageNamed:@"topbar_icon_search_normal"] forState:UIControlStateHighlighted];
    [searchbutton addTarget:self action:@selector(searchclick)forControlEvents:UIControlEventTouchDown];
    [rightBarView addSubview:searchbutton];
    
    UIButton *messagebutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [messagebutton setFrame:CGRectMake(40, 5, 25, 25)];
    [messagebutton setImage:[UIImage imageNamed:@"mymessage1"] forState:UIControlStateNormal];
    [messagebutton addTarget:self action:@selector(messageclick)forControlEvents:UIControlEventTouchDown];
    [rightBarView addSubview:searchbutton];
    [rightBarView addSubview:messagebutton];
    rightBarView.backgroundColor=[UIColor clearColor];
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithCustomView:rightBarView];
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    //左边两个按钮的创建
    UIView *leftBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 31)];
    
    UIButton *uploadbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    uploadbutton.frame=CGRectMake(0, 5, 25, 25);
    [uploadbutton setImage:[UIImage imageNamed:@"topbar_icon_upload_pressed"] forState:UIControlStateNormal];
    [uploadbutton setImage:[UIImage imageNamed:@"topbar_icon_upload_normal"] forState:UIControlStateHighlighted];
    [uploadbutton addTarget:self action:@selector(uploadClick)forControlEvents:UIControlEventTouchDown];
    [leftBarView addSubview:uploadbutton];
    
    UIButton *downloadbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [downloadbutton setFrame:CGRectMake(30, 5, 25, 25)];
    [downloadbutton setImage:[UIImage imageNamed:@"download-select"] forState:UIControlStateNormal];
    [downloadbutton setImage:[UIImage imageNamed:@"download"] forState:UIControlStateHighlighted];
    [downloadbutton addTarget:self action:@selector(downLoadClick)forControlEvents:UIControlEventTouchDown];
    [leftBarView addSubview:uploadbutton];
    [leftBarView addSubview:downloadbutton];
    leftBarView.backgroundColor=[UIColor clearColor];
    
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithCustomView:leftBarView];
    self.navigationItem.leftBarButtonItem = leftBtn;

    //图片滚动区域
    UIImageView *backgroundView = [[UIImageView alloc] init];
    backgroundView.frame = CGRectMake(0, 0, w, 310);
    [self.view addSubview:backgroundView];
    
    UIScrollView *demoContainerView = [[UIScrollView alloc] initWithFrame:backgroundView.frame];
    demoContainerView.contentSize = CGSizeMake(self.view.frame.size.width, 0);
    [self.view addSubview:demoContainerView];
    //采用网络图片实现
    NSArray *imagesURLStrings = @[
                                  @"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a4b3d7085dee3d6d2293d48b252b5910/0e2442a7d933c89524cd5cd4d51373f0830200ea.jpg",
                                  @"https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a41eb338dd33c895a62bcb3bb72e47c2/5fdf8db1cb134954a2192ccb524e9258d1094a1e.jpg",
                                  @"http://c.hiphotos.baidu.com/image/w%3D400/sign=c2318ff84334970a4773112fa5c8d1c0/b7fd5266d0160924c1fae5ccd60735fae7cd340d.jpg"
                                  ];
    SDCycleScrollView *cycleScrollView3 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 64, w, 200) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
    cycleScrollView3.currentPageDotImage = [UIImage imageNamed:@"pageControlCurrentDot"];
    cycleScrollView3.pageDotImage = [UIImage imageNamed:@"pageControlDot"];
    cycleScrollView3.imageURLStringsGroup = imagesURLStrings;
    
    [demoContainerView addSubview:cycleScrollView3];
}

-(void)keyboardHide:(UITapGestureRecognizer*)tap{
    [_searchBar resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 264, self.view.frame.size.width, self.view.frame.size.height-264) style:UITableViewStyleGrouped];
    _tableview.delegate =self;
    _tableview.dataSource=self;
    [self.view addSubview:_tableview];
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    if(index == 0) {
        NSLog(@"点击了第一张图片");
    }
    if(index == 1) {
        NSLog(@"点击了第二张图片");
    }
    if(index == 2) {
        NSLog(@"点击了第三张图片");
    }
}

- (void)uploadClick{
    UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:@"选择方式" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"本地视频" otherButtonTitles:@"摄像机", nil];
    [actionsheet showInView:self.view];
}

- (void)downLoadClick{
    DownloadViewController *download = [[DownloadViewController alloc] initWithNibName:@"DownloadViewController" bundle:nil];
    [self.navigationController pushViewController:download animated:YES];
}

- (void)searchclick {
    _alertview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"暂时没有商品上架!" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(dissAlert) userInfo:nil repeats:NO];
    [_alertview show];
}

- (void)dissAlert {
    [self.alertview dismissWithClickedButtonIndex:0 animated:NO];
}

- (void)messageclick {
    _alertview = [[UIAlertView alloc] initWithTitle:nil message:@"暂时没有新消息!" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(dissAlert) userInfo:nil repeats:NO];
    [_alertview show];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:identifier];
        cell.textLabel.text = @"111";
    }
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if(section == 0) {
        return @"热门文章";
    }else
        return @"热门视频";
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
